<template>
  <el-dialog :title="titleText" :visible="showDialog" @close="btnCancel">
    <el-form ref="addEmployee" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
          @blur="checkManager"
        >
          <el-option
            v-for="employee in employeeList"
            :key="employee.id"
            :label="employee.username"
            :value="employee.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="add">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row></el-dialog>
</template>

<script>
import { getDepartments, addDepartment, getDepartmentDetail, editDepartment } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      let isRepeat = false
      // 判断逻辑
      // 1. 获取到所有的部门列表
      const { depts } = await getDepartments()
      if (this.formData.id) {
        isRepeat = depts
        // 2. 筛选出当前被点击部门下属的同级部门
          .filter(item => item.pid === this.nodeData.id)
        // 3. 校验是否重名
          .some(item => item.name === value && item.id !== this.nodeData.id)
      } else {
        isRepeat = depts
          .filter(item => item.pid === this.nodeData.id)
          .some(item => item.name === value)
      }

      // 后续处理
      if (isRepeat) {
        callback(new Error('同一部门下不能同名'))
      } else {
        callback()
      }
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      let isRepeat = false
      // 1. 获取全部列表
      const { depts } = await getDepartments()
      // 2. 判断是否有相同 code (忽略父部门)
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === value && item.id !== this.nodeData.id)
      } else {
        isRepeat = depts.some(item => item.code === value)
      }
      // 后续处理
      if (isRepeat) {
        callback(new Error('全公司内代码必须唯一'))
      } else {
        callback()
      }
    }
    return {
      employeeList: [],
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '编码必须在50个字符以内', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        introduce: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 300, message: '介绍必须在300个字符以内', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '名称必须在50个字符以内', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }
        ]
      }
    }
  },
  computed: {
    titleText() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.employeeList = await getEmployeeSimple()
    },
    checkManager() {
      setTimeout(() => {
        this.$refs.addEmployee.validateField('manager')
      }, 100)
    },
    async   add() {
      if (this.formData.id) {
        await editDepartment(this.formData)
      } else {
        await addDepartment({ ...this.formData, pid: this.nodeData.id })
      }
      this.$message.success('添加成功')
      this.$emit('update:showDialog', false)
      this.$emit('addDepartment')
    },
    btnCancel() {
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      this.$refs.addEmployee.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    async getDepartmentDetail(id) {
      this.formData = await getDepartmentDetail(id)
    }
  }
}
</script>

<style>

</style>
